package com.saicmotor.pay.http;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.saicmotor.pay.http.Entity.QueryTranStatusEntity;
import com.saicmotor.pay.http.Entity.appendPayRecordEntity;
import com.saicmotor.pay.http.request.appendPayRecordReq;
import com.saicmotor.pay.http.request.queryTranStatusReq;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PayApiManager {
    private static final String BASE_URL = " https://acp.z-onesoftware.com/";
    private static final int CONNECT_TIME_OUT = 15;
    private static final int WRITE_TIME_OUT = 15;
    private static final int READ_TIME_OUT = 15;

    public static class HeaderInterceptor implements Interceptor {
        @NotNull
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Request build = request.newBuilder()
                    .build();
            return chain.proceed(build);
        }
    }

    private OkHttpClient mOkHttpClient;
    private Retrofit mRetrofit;
    private Pay_Interface mApi;

    public static PayApiManager getInstance() {
        return DInteractionAPIDelegateHolder.instance;
    }

    private PayApiManager() {
        init();
    }

    private static class DInteractionAPIDelegateHolder {
        private static final PayApiManager instance = new PayApiManager();
    }

    private void init() {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
                .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIME_OUT, TimeUnit.SECONDS);
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
        HeaderInterceptor headerInterceptor = new HeaderInterceptor();
        builder.addInterceptor(headerInterceptor);

        mOkHttpClient = builder.build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(mOkHttpClient)
                .build();
        mApi = mRetrofit.create(Pay_Interface.class);
    }


    //下支付单
    public Observable<BaseResponse<appendPayRecordEntity>> appendPayRecord(String transId,
                                                                           String platformMerchId,
                                                                           String merchOrderId,
                                                                           String userId,
                                                                           String userName,
                                                                           Float orderAmt,
                                                                           Float transAmt,
                                                                           String goodsDescription,
                                                                           String timeExpire,
                                                                           String payScene,
                                                                           List<appendPayRecordReq.payDetails> payDetails,
                                                                           String payerClientIp) {
        appendPayRecordReq request = new appendPayRecordReq();
        request.transId = transId;
        request.platformMerchId = platformMerchId;
        request.merchOrderId = merchOrderId;
        request.userId = userId;
        request.userName = userName;
        request.orderAmt = orderAmt;
        request.transAmt = transAmt;
        request.goodsDescription = goodsDescription;
        request.timeExpire = timeExpire;
        request.payScene = payScene;
        request.payDetails = payDetails;
        request.payerClientIp = payerClientIp;
        return mApi.appendPayRecord(request);
    }

    //订单轮询
    public Observable<BaseResponse<QueryTranStatusEntity>> queryTranStatus(String merchOrderId, String platformMerchId, String transId) {
        queryTranStatusReq request = new queryTranStatusReq();
        request.merchOrderId = merchOrderId;
        request.platformMerchId = platformMerchId;
        request.transId = transId;
        return mApi.queryTranStatus(request);
    }
}
